/* --COPYRIGHT--,BSD
 * Copyright (c) 2012, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * --/COPYRIGHT--*/
#include "uart.h"
#include "led.h"

//******************************************************************************
//!
//! This example shows how to configure the UART module to receive data in
//! UART mode and echo received character
//!
//!                MSP430F5438A
//!             -----------------
//!         /|\|                 |
//!          | |                 |
//!          --|RST              |
//!            |                 |
//!            |     P3.4/UCA0TXD|------------>
//!            |                 | 9600
//!            |     P3.5/UCA0RXD|<------------
//!
//!
//! This example uses the following peripherals and I/O signals.  You must
//! review these and change as needed for your own board:
//! - UART peripheral
//! - GPIO Port peripheral (for UART pins)
//! - UCA0TXD
//! - UCA0RXD
//!
//! This example uses the following interrupt handlers.  To use this example
//! in your own application you must add these interrupt handlers to your
//! vector table.
//! - USCI_A0_VECTOR.
//******************************************************************************

//*****************************************************************************
//
//Initialize received data
//
//*****************************************************************************
#include "mystring.h"


#define UART_DEBUG
#define LED_DEBUG

#ifdef LED_DEBUG
#include "led.h" 
#endif

#ifdef LED_DEBUG
#define LED1_RESET() LED1_reset()
#define LED2_RESET() LED2_reset()
#define LED1_TOGGLE() LED1_toggle()
#define LED2_TOGGLE() LED2_toggle()
#define LED1_SET() LED1_set()
#define LED2_SET() LED2_set()
#else
#define LED1_RESET()
#define LED2_RESET() 
#define LED1_TOGGLE()
#define LED2_TOGGLE() 
#define LED1_SET() 
#define LED2_SET()
#endif

#ifdef UART_DEBUG
#include "uart.h"
#define PRINT_CHAR(x) print_char(x) 
#define PRINT(x) print(x)
#define PRINT_RF(x, length) print_rf(x, length)
#else
#define PRINT_CHAR(x)
#define PRINT(x)
#define PRINT_RF(x, length)
#endif

typedef enum {  TURE = 0, FALSE = !TURE } FlagStatus;

#ifdef UART_DEBUG
volatile FlagStatus uart_receive_flag = TURE;
#endif

unsigned char rxBuffer[64];

void main(void)
{
	//Stop WDT
    WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer

    P5SEL |= BIT4 + BIT5;                       // Port select XT1
    
    UART_config();
    LED_config();

	PRINT("hello world\n");

    //Enter LPM3 mode with interrupts enabled
    __bis_SR_register(GIE);
   
    while (1) {
        if (uart_receive_flag == FALSE) {
            uart_receive_flag = TURE;
            PRINT_RF(rxBuffer, rxBuffer[0]);
            UART_enableInterrupt(UCRXIE);
            LED1_TOGGLE();
            __no_operation();
        }
    }
}


#ifdef UART_DEBUG

unsigned char k = 0x00;
unsigned char c = 0x00;

#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR (void)
{
    switch (__even_in_range(UCA1IV,4)){
        //Vector 2 - RXIFG
        case 2:

            //USCI_A0 TX buffer ready?
            while (!UART_getInterruptStatus(UCTXIFG));
            c = UART_receiveData();
//            PRINT_CHAR(c);
			TxBuffer[k] = c;
	
			if (TxBuffer[0] <= k) {
				uart_receive_flag = FALSE;
				UART_disableInterrupt(UCRXIE);
				str_cpy(rxBuffer, TxBuffer, TxBuffer[0]);
			    str_set(TxBuffer, 64, 0x00);
                PRINT("--------");
                k = 0;
            } else {
                if (k++ > 64) {
                    str_set(TxBuffer, 64, 0x00);
                    k = 0;
                }				
			}
			
            break;
        default: break;
    }
}

#endif